package com.hxcoder.crowd.handler;

import com.hxcoder.crowd.api.MySQLRemoteService;
import com.hxcoder.crowd.constant.CrowdConstant;
import com.hxcoder.crowd.entity.vo.AddressVO;
import com.hxcoder.crowd.entity.vo.MemberLoginVO;
import com.hxcoder.crowd.entity.vo.OrderProjectVO;
import com.hxcoder.crowd.util.ResultEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author hxcoder
 */
@Controller
public class OrderHandler {

    private Logger logger = LoggerFactory.getLogger(OrderHandler.class);

    @Resource
    private MySQLRemoteService mySQLRemoteService;

    /**
     * 发起订单——第二步：保存地址信息
     *
     * @param addressVO
     * @param session
     * @return
     */
    @RequestMapping("save/address")
    public String saveAddress(AddressVO addressVO, HttpSession session) {
        // 1.执行地址信息的保存
        ResultEntity<String> resultEntity = mySQLRemoteService.saveAddressRemote(addressVO);

        // 2.获取session中的 orderProjectVO 再获取用户的id然后重定向到订单页面
        OrderProjectVO orderProjectVO = (OrderProjectVO) session.getAttribute("orderProjectVO");
        Integer returnCount = orderProjectVO.getReturnCount();
        return "redirect:http://localhost:80/order/confirm/order/" + returnCount;
    }

    @RequestMapping("confirm/return/info/{projectId}/{returnId}")
    public String showReturnConfirmInfo(@PathVariable("projectId") Integer projectId, @PathVariable("returnId") Integer returnId, HttpSession session) {

        ResultEntity<OrderProjectVO> resultEntity = mySQLRemoteService.getOrderProjectVORemote(projectId, returnId);
        if (ResultEntity.SUCCESS.equals(resultEntity.getResult())) {
            OrderProjectVO orderProjectVO = resultEntity.getData();
            session.setAttribute("orderProjectVO", orderProjectVO);
        }
        return "confirm_return";
    }

    /**
     * 发起订单——第二步：接收回报数量，查询收货地址，并返回
     *
     * @param returnCount
     * @param session
     * @return
     */
    @RequestMapping("/confirm/order/{returnCount}")
    public String showConfirmOrderInfo(@PathVariable("returnCount") Integer returnCount, HttpSession session) {

        // 1.把接收到的回报数量合并到session
        OrderProjectVO orderProjectVO = (OrderProjectVO) session.getAttribute("orderProjectVO");
        orderProjectVO.setReturnCount(returnCount);
        session.setAttribute("orderProjectVO", orderProjectVO);

        // 2.获取当前已登录的用户id
        MemberLoginVO memberLoginVO = (MemberLoginVO) session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER);
        Integer memberId = memberLoginVO.getId();

        // 3.查询目前收货地址的数据
        ResultEntity<List<AddressVO>> resultEntity = mySQLRemoteService.getAddressVORemote(memberId);
        if (ResultEntity.SUCCESS.equals(resultEntity.getResult())) {
            List<AddressVO> list = resultEntity.getData();
            session.setAttribute("addressVOList", list);
        }
        return "confirm_order";
    }
}
